package com.wentao.thisService.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 拦截器
 */
@Component
public class AuthenticationInterceptor extends HandlerInterceptorAdapter {

//    private static final Logger LOGGER = LogManager.getLogger(AuthenticationInterceptor.class);
    //private static final String[] ALLOW_HEADS = {"app-id", "serial-number", "request-time", "device-id", "app-version", "market", "sign", "customer-id", "access-token", "os-version", "phone-models", "app-stage", "X-Token", "content-type"};

//    @Autowired
//    private AuthorityServiceUserInterface authorityServiceUserInterface;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
        response.addHeader("Access-Control-Allow-Origin", "*");
        response.addHeader("Access-Control-Allow-Methods", "*");
        response.addHeader("Access-Control-Max-Age", "100");
        response.addHeader("Access-Control-Allow-Headers", "*");
        response.addHeader("Access-Control-Allow-Credentials", "true");
//        String requestURI = request.getRequestURI();
//        String appURI = "/app";
//        String fileURI = "/file";
//        if (requestURI.contains(appURI) || requestURI.contains(fileURI)) {
//            ServletInputStream inputStream = request.getInputStream();
//            String bodyString = IOUtils.toString(inputStream);
//            RequestBase requestBase = JsonUtils.toBean(bodyString, RequestBase.class);
//            if (null != requestBase) {
//                String userIdString = requestBase.getUserId();
//                String token = requestBase.getToken();
//                if (StringUtils.isBlank(userIdString)) {
////                    response.getWriter().write(JsonUtils.toString(ResultBase.ok(ExceptionCode.USERID_EMPTY_ERROR)));
//                    response.sendError(1023, "userId为空");
//                    return false;
//                }
//                long userId = Long.parseLong(userIdString);
//                ResultBase resultBase = authorityServiceUserInterface.checkLogin(userId, token);
//                if (ExceptionCode.USER_NOT_EXIST.getCode().equals(resultBase.getResultCode())) {
////                    response.getWriter().write(JsonUtils.toString(resultBase));
//                    response.sendError(1004, "用户不存在");
//                    return false;
//                }
//                if (ExceptionCode.TOKEN_EXPIRE_ERROR.getCode().equals(resultBase.getResultCode())) {
////                    response.getWriter().write(JsonUtils.toString(resultBase));
//                    response.sendError(1022, "token已过期");
//                    return false;
//                }
//            }
//        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        super.afterCompletion(request, response, handler, ex);
    }
}
